www.gusucode.com > XerCMS 携云PHP企业建站程序 v2015PHP源码程序 > XerCMS 携云PHP企业建站程序 v2015/XerCMS_v20150724/XerCMS_v20150724/XerCMS/Library/XerCMS_upload.php

    <?php
/**
 * @name     XERCMS
 * @author   Arno <XerCMS@163.com> [QQ:1328013]
 * @version  1.0.0
 * @link     http://www.XerCMS.com 
 */

!defined('XERCMS') && exit('Access Denied');

class UPLOAD
{
     var $config;
     var $forbid = array('php','asp','aspx','vbs','bat','asa');
     var $result;
	var $paths;
	var $cid;
	var $helper = null;
	var $image = null;
	
     function __construct() {

     }
  
     function handle($file,$ext) {
	     if(in_array($ext,array('jpg' , 'bmp' , 'gif' , 'png' , 'jpeg'))) {
			if($this->image == null) {
                    include_once(dirname(__file__).'/upload/XerCMS_image.php');
		          $this->image = new image();
			     $this->image->Init($this->config);		    
			}
			$locals = $this->image->handle($file);
			return $locals;    
	     }
		return null;
	}
	
     function dir($dir,$rid,$ext) {
		$this->paths[$rid] = $dir.date('Y/md/',X::$G['time']).ceil($rid/500).'/';
          DIR::mkdirs($this->paths[$rid]);
		$this->paths[$rid] .= $rid.'.'.$ext;
     }

     function name($rid) {
	     return $this->paths[$rid];
     }
 
     function load($id) {
	     $this->cid = $id;
          $this->config = ini('member/upload/'.$id);
          if(defined('CKY')) {
               $this->config['maxsize'] = 0;
          }
     }
  	
	function files() {
    		foreach($_FILES as $k=>$v) {	
			$this->file($k);
		}
	}
	
     function conf($id,$data) {
          $this->cid = $id;
          $this->config = $data;
          if(defined('CYK')) {
               $this->config['maxsize'] = 0;
          }
     }
     
	function file($name) { 
	     global $G;
		if(isset($_FILES[$name]['tmp_name']) && !empty($_FILES[$name]['tmp_name'])) {
			$ext = $this->ext($_FILES[$name]['name']);
               if(in_array(strtolower($ext),$this->forbid) || preg_match('/([^a-z0-9])/i',$ext,$match)) { 	   
                    $this->result[$name]['error'] = 'Ext';return;
               }	
			if(!empty($this->config['maxsize']) && $_FILES[$name]['size'] > $this->config['maxsize']) {
				$this->result[$name]['error'] = 'Size';return;
			}
	          $rid = $this->record($_FILES[$name]);
			$this->dir($this->config['path'],$rid,$ext);
			if(is_uploaded_file($_FILES[$name]['tmp_name'])) {
                    if(move_uploaded_file($_FILES[$name]['tmp_name'],$this->name($rid)) == false) {
                         $this->delrid($rid);
                         $this->result[$name]['error'] = 'Move';return;
                    } else {
				     //chmod($this->name($rid),0644);
				}
				if($this->config['local'] == '0') {
                    	if(!REMOTE::upload($this->name($rid),$this->name($rid),$this->config['remote'],$this->config['mode'])) {
                         	$this->delrid($rid);@unlink($this->name($rid));
                         	$this->result[$name]['error'] = $this->config['mode'];return;
                    	}
               	}
				//if($helper != null) {
					$files = $this->handle($this->name($rid),$ext);	
					if($files != null && $this->config['local'] == '0') {
					     foreach($files as $file) {
                    			if(!REMOTE::upload($file,$file,$this->config['remote'],$this->config['mode'])) {
                         			$this->delrid($rid);
                         			$this->result[$name]['error'] = 'extra';	
								break;
                    			}						
						}
						if(!empty($this->result[$name]['error'])) {
						     foreach($files as $file) {
							     @unlink($file);
							}
						     return;
						}
					}
				//}	
               }
			$this->result[$name]['host'] = $this->config['host'];
               $this->result[$name]['path'] = $this->name($rid);
               $this->setPath($rid,$this->result[$name]['path']);
		} else $this->result[$name]['error'] = 'Tmp';
	}
  
     function show() {
	     $msg = array('XerCMS'=>'Success','data'=>$this->result);
          exit(json_encode($msg));
     }
 
     function ext($file) {
          $file = explode('.',$file);
          return $file[(count($file) - 1)];
     }
  
     function delrid($id) {
          DB::query('DELETE FROM xercms_member_upfiles WHERE id = '.$id.' AND uid = '.X::$G['uid'].' LIMIT 1');
     }
  
     function setPath($id,$url) {
          DB::update('xercms_member_upfiles',array('url'=>$url),'id = \''.$id.'\' LIMIT 1'); 
     }
  
     function record($upfile) {
		if (X::$G['uid']) {
               DB::add('xercms_member_count',array('upload'=>$upfile['size']),array('uid'=>X::$G['uid']));
		}
          DB::insert('xercms_member_upfiles',
	         array('uid'=>X::$G['uid'],
				'size'=>$upfile['size'],
				'name'=>$upfile['name'],
				'time'=>X::$G['time'],
				'ip'=>X::$G['ip'],
				'type'=>$this->cid));
          return DB::lastid(); 
     }
}
?>